package com.yxd.bdmapdemo.base;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.yxd.bdmapdemo.utils.L;
import com.yxd.bdmapdemo.utils.NetUtils;
import com.yxd.bdmapdemo.utils.SystemUtil;
import com.yxd.bdmapdemo.utils.UserManager;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by dong on 2017/5/9.
 */

public class BaseNetActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    /* params.addHeader("version", SystemUtil.getAppInfo());//客户端版本version
        params.addHeader("token", UserManager.getInstance().getUserToken());
        params.addHeader("from","android");
        params.addHeader("os", SystemUtil.getSystemVersion());//手机系统版本（Build.VERSION.RELEAS）例：4.4，4.5
        if (TextUtils.isEmpty(Constant.sreenSize)) {
            SharedPreferences sp = UIUtils.getContext().getSharedPreferences("screen_size", Context.MODE_PRIVATE);
            Constant.sreenSize = sp.getString("screen_size", "");
            // id = sp.getInt("screen_size", 0);
        }
        params.addHeader("screen","1920x1080");// Constant.sreenSize
        params.addHeader("model", SystemUtil.getSystemModel());//机型信息（Build.MODEL），例：Redmi Note 3
        params.addHeader("channel", "com.wandoujia");//渠道信息，例：com.wandoujia
        params.addHeader("net", NetUtils.getNetWorkType());
        params.addHeader("appid", "1");//APP唯一标识
        params.addHeader("uid",""+UserManager.getInstance().getUserId());
        if (TextUtils.isEmpty(Constant.deviceId)) {
            SharedPreferences sp = UIUtils.getContext().getSharedPreferences("device_info", Context.MODE_PRIVATE);
            String mac = sp.getString("mac", "");
            String androidId = sp.getString("android_id", "");
            Constant.deviceId = SystemUtil.getDeviceId(mac, androidId);
        }
        params.addHeader("deviceid", Constant.deviceId);//设备id*/

    protected void request(String url, String request) {
        L.e("request:  "+ request);
        url = Constant.URLS.BASEURL + url;

        HashMap<String, String> headers = new HashMap<>();
                headers.put("version", SystemUtil.getAppInfo());//客户端版本version
                headers.put("token", UserManager.getInstance().getUserToken());
                headers.put("from", "android");
                headers.put("os", SystemUtil.getSystemVersion());//手机系统版本（Build.VERSION.RELEAS）例：4.4，4.5
                headers.put("screen", "1920x1080");// Constant.sreenSize
                headers.put("model", SystemUtil.getSystemModel());//机型信息（Build.MODEL），例：Redmi Note 3
                headers.put("channel", "com.wandoujia");//渠道信息，例：com.wandoujia
                headers.put("net", NetUtils.getNetWorkType());
                headers.put("appid", "1");//APP唯一标识
                headers.put("uid", "" + UserManager.getInstance().getUserId());
                headers.put("deviceid", Constant.deviceId);//设备id*/


        OkHttpUtils
                .postString()
                .url(url)
                .headers(headers)
      /*  .addParams("username", "hyman")
                .addParams("password", "123")*/
                .content(request)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        L.e("onBefore  "  + Thread.currentThread().getName()+"----"+request+"---"+id);
                    }

                    @Override
                    public void onAfter(int id) {
                        super.onAfter(id);
                        L.e("onAfter  "  + Thread.currentThread().getName()+"---"+id);

                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        L.e("onError  "  + Thread.currentThread().getName()+"---"+id);
                        fail();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        L.e("onResponse  "  + Thread.currentThread().getName()+"---"+id+"--"+response);
                        success();
                    }

                    @Override
                    public String parseNetworkResponse(Response response, int id) throws IOException {
                        L.e("parseNetworkResponse  "  + Thread.currentThread().getName()+"---"+id+"----"+response);
                        parse();
                        return super.parseNetworkResponse(response, id);
                    }
                });
    }

    protected void fail() {
    }

    protected void success() {
    }

    protected void parse() {
    }
}
